* Make Figures A5 and A6

* Build estimates for PCI (365 days)
use "$savedata/masterdata.dta", replace

merge m:1 trust_code using "$savedata/connect_total25_pcihosps.dta"
keep if _merge==3

gen pat_pci = 0
replace pat_pci = 1 if substr(opertn_01,1,3)=="K49" | substr(opertn_01,1,3)=="K50" | substr(opertn_01,1,3)=="K75"

egen sum_pci = sum(pat_pci), by(trust_code finyear)
keep if sum_pci>10

cap drop vol* sample*

egen vol_trust = sum(one), by(pconsult trust_num)

foreach x in 10 25 50 100{
egen vol`x' = sum(one) if akm_set_mainspef`x'==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'==1 & vol_trust>=`x'
}

keep if sample25==1
gen vol = vol25

reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

rename docfe docfe365_pci
rename adj_docfe adj_docfe365_pci

save "$savedata/365pci_estimates.dta", replace

* Build estimates for PCI (30 days)
use "$savedata/masterdata.dta", replace

merge m:1 trust_code using "$savedata/connect_total25_pcihosps.dta"
keep if _merge==3

gen pat_pci = 0
replace pat_pci = 1 if substr(opertn_01,1,3)=="K49" | substr(opertn_01,1,3)=="K50" | substr(opertn_01,1,3)=="K75"

egen sum_pci = sum(pat_pci), by(trust_code finyear)
keep if sum_pci>10

cap drop vol* sample*

egen vol_trust = sum(one), by(pconsult trust_num)

* Doctor FEs

foreach x in 10 25 50 100{
egen vol`x' = sum(one) if akm_set_mainspef`x'==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'==1 & vol_trust>=`x'
}

keep if sample25==1
gen vol = vol25

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

rename docfe docfe30_pci
rename adj_docfe adj_docfe30_pci

save "$savedata/30pci_estimates.dta", replace


* Build no prev interactions (30 days)

use "$savedata/masterdata.dta", clear

merge m:1 extract using "$savedata/no_past_interactions.dta"
keep if _merge==3
keep if match==0
drop _merge

merge m:1 trust_code using "$savedata/connect_total25_noprevmatch.dta"
keep if _merge==3
drop _merge


drop vol* sample*
egen vol_trust = sum(one), by(pconsult trust_num)

foreach x in 25{
egen vol`x' = sum(one) if akm_set_mainspef`x'_nomatch==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'_nomatch==1 & vol_trust>=`x'
}

keep if sample25==1
gen vol = vol25

* Doctor FEs

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step 

rename docfe docfe30_noprev
rename adj_docfe adj_docfe30_noprev

save "$savedata/30noprev_estimates.dta", replace


* Build estimates with no prev estimates (365 days)

use "$savedata/masterdata.dta", clear

merge m:1 extract using "$savedata/no_past_interactions.dta"
keep if _merge==3
keep if match==0
drop _merge

merge m:1 trust_code using "$savedata/connect_total25_noprevmatch.dta"
keep if _merge==3
drop _merge


drop vol* sample*
egen vol_trust = sum(one), by(pconsult trust_num)

foreach x in 25{
egen vol`x' = sum(one) if akm_set_mainspef`x'_nomatch==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'_nomatch==1 & vol_trust>=`x'
}

keep if sample25==1
gen vol = vol25

* Doctor FEs
reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

rename docfe docfe365_noprev
rename adj_docfe adj_docfe365_noprev

save "$savedata/365noprev_estimates.dta", replace


*** Then produce main estimates
use "$savedata/masterdata.dta", clear

keep if sample25==1
gen vol = vol25

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe30, u
gen sigma_u30 = e(sigma_u)
gen sigma_e30 = e(sigma_e)

reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual365, residuals

xtreg residual365 c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe365, u
gen sigma_u365 = e(sigma_u)
gen sigma_e365 = e(sigma_e)

collapse (mean) docfe* sigma* vol, by(doctor_id)

foreach x in 30 365{
gen signal_2step`x' = ((sigma_u`x'^2) / ((sigma_u`x'^2) + ((sigma_e`x'^2)/vol)))
gen adj_docfe`x' = docfe`x'*signal_2step`x'

rename docfe`x' docfe`x'_all
rename adj_docfe`x' adj_docfe`x'_all

}


* Merge in other measures

foreach x in 30 365{
merge 1:1 doctor_id using "$savedata/`x'pci_estimates.dta"
rename _merge _merge`x'_pci

merge 1:1 doctor_id using "$savedata/`x'noprev_estimates.dta"
rename _merge _merge`x'_noprev
}


* Correlations between the different measures as reported in the text in Section 5.2.1
corr adj_docfe30_all adj_docfe30_pci
corr adj_docfe365_all adj_docfe365_pci

corr adj_docfe30_all adj_docfe30_noprev
corr adj_docfe365_all adj_docfe365_noprev

twoway scatter adj_docfe30_all adj_docfe30_pci, mcolor(gs3)  || lfit adj_docfe30_all adj_docfe30_pci, ytitle(Estimated doctor quality (full sample)) xtitle(Estimated doctor quality (PCI hospitals only)) graphregion(color(white)) ///
legend(off)

graph export "$results/figureA5a.pdf", as(pdf) replace

twoway scatter adj_docfe365_all adj_docfe365_pci, mcolor(gs3)  || lfit adj_docfe365_all adj_docfe365_pci, ytitle(Estimated doctor quality (full sample)) xtitle(Estimated doctor quality (PCI hospitals only)) graphregion(color(white)) ///
legend(off)
graph export "$results/figureA5b.pdf", as(pdf) replace

twoway scatter adj_docfe30_all adj_docfe30_noprev, mcolor(gs3)  || lfit adj_docfe30_all adj_docfe30_noprev, ytitle(Estimated doctor quality (full sample)) xtitle(Estimated doctor quality (No past interactions)) graphregion(color(white)) ///
legend(off)

graph export "$results/figureA6a.pdf", as(pdf) replace

twoway scatter adj_docfe365_all adj_docfe365_noprev, mcolor(gs3)  || lfit adj_docfe365_all adj_docfe365_noprev, ytitle(Estimated doctor quality (full sample)) xtitle(Estimated doctor quality (No past interactions)) graphregion(color(white)) ///
legend(off)
graph export "$results/figureA6b.pdf", as(pdf) replace

